<script setup lang="ts">
import type { ConsultOrderItem } from '@/types/consult'
import { OrderType } from '@/enums'
import ConnsultMore from './ConsultMore.vue'
import { useCancelConsult } from '@/composables/useCancelConsult'
import { useDelConsult } from '@/composables/useDelConsult'
import { usePrescription } from '@/composables/usePrescription'

const props = defineProps<{
  orderItem: ConsultOrderItem
  onDel: (id: string) => void
}>()

const { loading, handleCancelConsult } = useCancelConsult(props.orderItem)
const { delLoading, handleDel } = useDelConsult(props.orderItem, props.onDel)
const { previewPrescription } = usePrescription()

import { useRouter } from 'vue-router'
const router = useRouter()
const jumpToDetail = () => {
  router.push('/user/consult/' + props.orderItem.id)
}
</script>

<template>
  <div class="consult-item">
    <div class="head van-hairline--bottom">
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p>{{ orderItem.docInfo?.name }}</p>
      <span
        :class="{
          orange: orderItem.status === OrderType.ConsultPay,
          green: orderItem.status === OrderType.ConsultChat
        }"
        >{{ orderItem.statusValue }}</span
      >
    </div>
    <div class="body" @click="jumpToDetail">
      <div class="body-row">
        <div class="body-label">病情描述</div>
        <div class="body-value">{{ orderItem.illnessDesc }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">价格</div>
        <div class="body-value">¥ {{ orderItem.payment.toFixed(2) }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">创建时间</div>
        <div class="body-value tip">{{ orderItem.createTime }}</div>
      </div>
    </div>
    <!-- 待支付 -->
    <div class="foot" v-if="orderItem.status === OrderType.ConsultPay">
      <van-button
        class="gray"
        plain
        size="small"
        round
        :loading="loading"
        @click="handleCancelConsult()"
        >取消问诊</van-button
      >
      <van-button type="primary" plain size="small" round>去支付</van-button>
    </div>
    <!-- 待就诊 -->
    <div class="foot" v-if="orderItem.status === OrderType.ConsultWait">
      <van-button
        class="gray"
        plain
        size="small"
        round
        :loading="loading"
        @click="handleCancelConsult()"
        >取消问诊</van-button
      >
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/room?orderId=${orderItem.id}`"
        >继续沟通</van-button
      >
    </div>
    <!-- 咨询中 -->
    <div class="foot" v-if="orderItem.status === OrderType.ConsultChat">
      <van-button
        class="gray"
        plain
        size="small"
        round
        v-if="orderItem.prescriptionId"
        @click="previewPrescription(orderItem.prescriptionId)"
        >查看处方</van-button
      >
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/room?orderId=${orderItem.id}`"
        >继续沟通</van-button
      >
    </div>
    <!-- 已取消 -->
    <div class="foot" v-if="orderItem.status === OrderType.ConsultCancel">
      <van-button
        class="gray"
        plain
        size="small"
        round
        :loading="delLoading"
        @click="handleDel()"
        >删除订单</van-button
      >
      <van-button type="primary" plain size="small" round
        >咨询其他医生</van-button
      >
    </div>
    <!-- 已完成 -->
    <div class="foot" v-if="orderItem.status === OrderType.ConsultComplete">
      <!-- TODO: 更多组件 -->
      <ConnsultMore
        :order-item="orderItem"
        @on-del="handleDel"
        @on-preview="previewPrescription"
      ></ConnsultMore>
      <van-button
        class="gray"
        plain
        size="small"
        round
        :to="`/room?orderId=${orderItem.id}`"
        >问诊记录</van-button
      >
      <van-button
        type="primary"
        plain
        size="small"
        round
        v-if="!orderItem.evaluateId"
        >写评价</van-button
      >
      <van-button type="primary" plain size="small" round v-else
        >查看评价</van-button
      >
    </div>
  </div>
</template>

<style lang="scss" scoped>
.consult-item {
  border-radius: 4px;
  box-shadow: 0px 0px 22px 0px rgba(245, 245, 245, 0.1);
  background-color: #fff;
  margin-bottom: 10px;
  .head {
    display: flex;
    align-items: center;
    height: 50px;
    padding: 0 15px;
    .img {
      width: 20px;
      height: 20px;
    }
    > p {
      flex: 1;
      font-size: 15px;
      padding-left: 10px;
    }
    > span {
      color: var(--cp-tag);
      &.orange {
        color: #f2994a;
      }
      &.green {
        color: var(--cp-primary);
      }
    }
  }
  .body {
    padding: 15px 15px 8px 15px;
    .body-row {
      display: flex;
      margin-bottom: 7px;
    }
    .body-label {
      width: 62px;
      font-size: 13px;
      color: var(--cp-tip);
    }
    .body-value {
      width: 250px;
      &.tip {
        color: var(--cp-tip);
      }
    }
  }
  .foot {
    padding: 0 15px 15px 15px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    .van-button {
      margin-left: 10px;
      padding: 0 16px;
      &.gray {
        color: var(--cp-text3);
        background-color: var(--cp-bg);
      }
    }
  }
}
</style>
